package educar.materia;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import educar.materia.EliminarMateriaVentana;

public class EliminarMateriaMediador implements ActionListener{
	
	private EliminarMateriaVentana view = new EliminarMateriaVentana();
	
	public EliminarMateriaMediador(){
		view.setVisible(true);
		view.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj instanceof JButton)
		  ListenerEvent(((JButton) obj).getName());
	}
	private void ListenerEvent(String event){
		if (event.compareTo("Aceptar") == 0){
			String codigo = view.getCodigo();
			if (MateriaController.existe(codigo)){
				MateriaController.delete(codigo);
				view.setCodigo();
				JOptionPane.showMessageDialog(null,"La materia fue eliminada con exito");
			} else {
				JOptionPane.showMessageDialog(null,"La materia no existe!");
				view.setCodigo();
			}
		}
		if (event.compareTo("Cancelar") == 0) {
			view.dispose();
		}
	}
}
